local shanxi = fk.CreateSkill {
  name = "mini__shanxiw",
}

Fk:loadTranslationTable{
  ["mini__shanxiw"] = "善檄",
  [":mini__shanxiw"] = "出牌阶段开始时，你可以令一名其他角色获得或失去“檄”标记；当有“檄”标记的角色回复体力后，"..
  "若其不处于濒死状态，其须选择一项：1.交给你两张牌；2.失去1点体力。",

  ["#mini__shanxiw-choose"] = "善檄：令一名角色获得或失去“檄”标记",
  ["@@mini__shanxiw"] = "檄",
  ["#mini__shanxiw-give"] = "善檄：请交给 %src 两张牌，否则失去1点体力",

  ["$mini__shanxiw1"] = "从军征遐路，讨彼东南夷。",
  ["$mini__shanxiw2"] = "身服干戈事，岂得念所私。",
}

shanxi:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(shanxi.name) and player.phase == Player.Play and
      #player.room:getOtherPlayers(player, false) > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = room:getOtherPlayers(player, false),
      skill_name = shanxi.name,
      prompt = "#mini__shanxiw-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:setPlayerMark(to, "@@mini__shanxiw", to:getMark("@@mini__shanxiw") > 0 and 0 or 1)
  end,
})

shanxi:addEffect(fk.HpRecover, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(shanxi.name) and target:getMark("@@mini__shanxiw") > 0 and
      not target.dying and not target.dead
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:doIndicate(player, {target})
    if #target:getCardIds("he") < 2 then
      room:loseHp(target, 1, shanxi.name)
      return
    end
    local cards = room:askToCards(target, {
      min_num = 2,
      max_num = 2,
      include_equip = true,
      skill_name = shanxi.name,
      prompt = "#mini__shanxiw-give:"..player.id,
      cancelable = true,
    })
    if #cards == 2 then
      room:moveCardTo(cards, Player.Hand, player, fk.ReasonGive, shanxi.name, nil, false, target)
    else
      room:loseHp(target, 1, shanxi.name)
    end
  end,
})

shanxi:addLoseEffect(function (self, player, is_death)
  local room = player.room
  for _, p in ipairs(room:getOtherPlayers(player, false)) do
    room:setPlayerMark(p, "@@mini__shanxiw", 0)
  end
end)

return shanxi
